package com.datn.activity;

import com.datn.DataAccess.UserDatabaseAdapter;
import com.datn.beans.UserBean;
import com.datn.utils.AlertCustom;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends Activity implements OnClickListener{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		Button btRegister=(Button) findViewById(R.id.btRegister);
		btRegister.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub


		switch (v.getId()) {
		case R.id.btRegister:
			//Click button Register Event
			processClickRegisterButton();
			break;

		default:
			break;
		}
	}



	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub

		if (keyCode==KeyEvent.KEYCODE_BACK){
			Intent intent=new Intent(this, NearLocationActivity.class);
			startActivity(intent);
			finish();
		}

		return super.onKeyDown(keyCode, event);
	}

	public void processClickRegisterButton() {

		EditText edtUsername=(EditText) findViewById(R.id.edtUsernameRegister);
		EditText edtPassword=(EditText) findViewById(R.id.edtPasswordRegister);
		EditText edtEmail=(EditText) findViewById(R.id.edtEmailRegister);
		EditText edtMobile=(EditText)findViewById(R.id.edtMobileRegister);

		String stUsername = edtUsername.getText().toString().trim();
		String stPassword = edtPassword.getText().toString().trim();
		String stEmail = edtEmail.getText().toString().trim();


		if (!((stUsername.equalsIgnoreCase(""))||
				(stPassword.equalsIgnoreCase(""))||
				(stEmail.equalsIgnoreCase("")))){
			UserDatabaseAdapter userAdapter=new UserDatabaseAdapter(this);
			userAdapter.open();
			if (userAdapter.isExistUser(stUsername)){
				/*AlertCustom.showAlert(this, "Notification", "Username: "+stUsername+" is exist");*/
				Toast.makeText(this, "Username: "+stUsername+" is exist", Toast.LENGTH_LONG).show();
			}
			else if (userAdapter.isExistEmail(stEmail)){
				/*AlertCustom.showAlert(this, "Notification", "Email : "+stEmail+" is already used");*/
				Toast.makeText(this, "Email : "+stEmail+" is already used", Toast.LENGTH_LONG).show();
			}
			else			
			{
				long result = userAdapter.createUser(stUsername, stPassword, stEmail);
				if (result!=-1){
					/*AlertCustom.showAlert(this, "Notification", "Register success");*/
					Toast.makeText(this, "Register success", Toast.LENGTH_LONG).show();
				}					
			}
			userAdapter.close();

		}
		else {
			/*AlertCustom.showAlert(this, "Notification", "Please input all field");*/
			Toast.makeText(this, "Please input all field", Toast.LENGTH_LONG).show();
		}
	}

}
